package com.jiawang.core.servlet;

import com.jiawang.core.constant.UriConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LiJiaWang
 * 创建时间: 14:29
 * 项目的名字: jio_writer_tomcat
 * <p style="color:#33CCFF"></p>
 */
public class ResponseBaseServlet {
    /**
     * 协议版本
     */
    private String version;
    /**
     * 状态码
     */
    private String code;
    /**
     * 响应头集合
     */
    private Map<String, String> header = new HashMap<>();

    /**
     * 响应报文
     */
    private StringBuilder sb = new StringBuilder();


    /**
     * 头部信息拼接
     */
    public void text() {

        this.sb.append(version).append(UriConstant.PLACEHOLDER).append(code).append(UriConstant.LINE_BREAK);
        for (Map.Entry<String, String> kv : header.entrySet()) {
            this.sb.append(kv.getKey()).append((UriConstant.COLON_SPACE)).append(kv.getValue()).append(UriConstant.LINE_BREAK);

        }
        // 必带两空行
        this.sb.append(UriConstant.ROW).append(UriConstant.LINE_BREAK);
    }


    /**
     * 设置响应头
     *
     * @param key   键
     * @param value 值
     */
    public void setHeader(String key, String value) {
        key = key + UriConstant.COLON_SPACE;
        this.header.put(key, value);
    }

    /**
     * 添加响应头
     *
     * @param key   键
     * @param value 值
     */
    public void addHeader(String key, String value) {
        this.setHeader(key, value);
    }

    public StringBuilder getSb() {
        return sb;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Map<String, String> getHeader() {
        return header;
    }

    public void setHeader(Map<String, String> header) {
        this.header = header;
    }


}
